package com.tinywind.admin.common.config.shiro;

import com.tinywind.admin.module.system.entity.SysUserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author liuxingyu01
 * @date 2021-01-07-23:09
 * @description shiro 工具类
 **/
public class ShiroUtils {
    private static final Logger logger = LoggerFactory.getLogger(ShiroUtils.class);

    private ShiroUtils() {
        throw new AssertionError();
    }


    /**
     * 获取当前用户Session
     *
     * @return Session
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }


    /**
     * 获取当前用户SessionId
     *
     * @return SessionId
     */
    public static String getSessionId() {
        return String.valueOf(getSession().getId());
    }


    /**
     * 获取登录用户
     *
     * @return SysUserInfo
     */
    public static SysUserInfo getSysUserInfo() {
        try {
            Subject subject = SecurityUtils.getSubject();
            SysUserInfo user = (SysUserInfo) subject.getPrincipal();
            if (user != null) {
                return user;
            }
        } catch (UnavailableSecurityManagerException e) {
            logger.error("ShiroUtils.getSysUserInfo:{e}", e);
        }
        return null;
    }

    /**
     * 获取登录用户ID
     *
     * @return id
     */
    public static String getSysUserId() {
        try {
            Subject subject = SecurityUtils.getSubject();
            SysUserInfo userInfo = (SysUserInfo) subject.getPrincipal();
            if (null != userInfo) {
                return userInfo.getUserId();
            }
        } catch (UnavailableSecurityManagerException e) {
            logger.error("ShiroUtils.getSysUserId:{e}", e);
        }
        return null;
    }

    /**
     * 获取登录用户账户account
     *
     * @return account
     */
    public static String getSysUserAccount() {
        try {
            Subject subject = SecurityUtils.getSubject();
            SysUserInfo userInfo = (SysUserInfo) subject.getPrincipal();
            if (null != userInfo) {
                return userInfo.getAccount();
            }
        } catch (UnavailableSecurityManagerException e) {
            logger.error("ShiroUtils.getSysUserId:{e}", e);
        }
        return null;
    }


    /**
     * 获取登录用户ip地址
     *
     * @return ip
     */
    public static String getIp() {
        return SecurityUtils.getSubject().getSession().getHost();
    }


    /**
     * 登陆用户登出
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }


    /**
     * 清理当前登陆用户权限缓存
     */
    public static void clearCachedAuthorizationInfo() {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        LoginRelam realm = (LoginRelam) rsm.getRealms().iterator().next();
        realm.clearCachedAuthorInfo();
    }


    /**
     * 根据userId清除当前session存在的用户的权限缓存
     *
     * @param accounts
     * @description 经测试，这个是有效的
     */
    public static void clearCachedAuthorInfoByAccounts(List<String> accounts) {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        LoginRelam realm = (LoginRelam) rsm.getRealms().iterator().next();
        realm.clearCachedAuthorInfoByAccounts(accounts);
    }


    /**
     * 根据用户account清除认证信息
     *
     * @param accounts 这个好像不起作用
     */
    public static void clearCachedAuthenInfo(List<String> accounts) {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        LoginRelam realm = (LoginRelam) rsm.getRealms().iterator().next();
        realm.clearCachedAuthenInfo(accounts);
    }


    /**
     * 根据account删除用户登陆信息，使他强制下线
     * 直接调用此方法，就可以使指定用户强制下线
     *
     * @param account
     */
    public static void forcedOffline(String account) {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        LoginRelam realm = (LoginRelam) rsm.getRealms().iterator().next();
        realm.forcedOffline(account);
    }

}
